package com.interview.b_并发.c_java_内存模型_JMM.d_可见性_volatile解决;

public class Test {
    volatile int a = 1;
    volatile int b = 2;
    private void write() {
        a = 3;
        b = a;
    }
    private void read() {
        System.out.println("b=" + b + ";a=" + a);
    }
    public static void main(String[] args) {
        for (int i = 0; i < 2000000; i++) {
            Test test = new Test();
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                    test.write();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                    test.read();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start(); } }}
